home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / mail / mailleds.93 / mailleds / mailleds-0.93 / leds.c < prev    next >
C/C++ Source or Header  |  1996-05-14  |  3KB  |  146 lines

  1. #include <unistd.h>
  2. #include <linux/kd.h>
  3. #include <sys/ioctl.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <signal.h>
  8.  
  9. #ifdef X_SUPPORT
  10. #include <X11/X.h>
  11. #include <X11/Xlib.h>
  12. #endif
  13.  
  14. #include "config.h"
  15. #include "mailleds.h"
  16. #include "port.h"
  17.  
  18. #define TTY_RECOVERABLE_ERROR 8
  19.  
  20. extern int ct[MAX_CONSOLES];
  21. extern char opt_t, opt_x, *opt_d;
  22. extern int x_leds[3];
  23.  
  24. #ifdef X_SUPPORT
  25. extern Display *dpy;
  26. #endif
  27.  
  28. extern int kbd_leds, port_leds;
  29. extern sig_atomic_t exit_now;
  30.  
  31. void set_kbd_leds(leds)
  32. int leds;
  33. {
  34.     int kbd_ledstate, x;
  35.     for (x = 0; ct[x] != -1; x++) {
  36.         kbd_ledstate = get_kbd_leds(ct[x]);
  37.         if (kbd_ledstate == TTY_RECOVERABLE_ERROR)
  38.             continue;
  39.         poke_kbd_leds(ct[x], leds | kbd_ledstate);
  40.     }
  41. }
  42.  
  43. void poke_kbd_leds(fd, leds)
  44. int fd, leds;
  45. {
  46.     if (ioctl(fd, KDSETLED, leds)) {
  47. #ifdef DEBUG
  48.         write(2, &"01"[(leds & 0x80) != 0], 1);
  49. #else
  50.         perror("mailleds: ioctl KDSETLED");
  51.         exit(1);
  52. #endif
  53.     }
  54. }
  55.  
  56. int get_kbd_leds(fd)
  57. int fd;
  58. {
  59.     char ledstate;
  60.     if (ioctl(fd, KDGETLED, &ledstate)) {
  61.         if (errno == EIO)
  62.             return (TTY_RECOVERABLE_ERROR);
  63.         perror("mailleds: ioctl KDGETLED");
  64.         /* EIO seems to be generated when logouts are happening nearly simultaneously. 
  65.            not serious.  Ignore, and make the other skip the tty. */
  66.         exit(1);
  67.     }
  68.     return (ledstate);
  69. }
  70.  
  71. #ifdef PARALLEL_SUPPORT
  72. void set_port_leds(port, leds)
  73. int port, leds;
  74. {
  75.     int state;
  76.     state = port_in(port);
  77.     port_out(port, leds | state);
  78. }
  79.  
  80. void unset_port_leds(port, leds)
  81. int port, leds;
  82. {
  83.     int state;
  84.     state = port_in(port);
  85.     port_out(port, leds ^ state);
  86. }
  87.  
  88. #endif                /* PARALLEL_SUPPORT */
  89.  
  90. #ifdef X_SUPPORT
  91. void set_x_leds(mode)
  92. int mode;
  93. {
  94.     int x;
  95.     XKeyboardControl kbd;
  96.     kbd.led_mode = mode;
  97.     for (x = 0; x_leds[x]; x++) {
  98.         kbd.led = x_leds[x];
  99.         XChangeKeyboardControl(dpy, KBLed | KBLedMode, &kbd);
  100.     }
  101. }
  102. #endif
  103.  
  104. /* theoretically, one of these days the pauses will be command line switches */
  105. void blink_once(on_pause, off_pause)
  106. unsigned long on_pause, off_pause;
  107. {
  108.     if (opt_t)
  109.         set_kbd_leds(kbd_leds);
  110. #ifdef X_SUPPORT
  111.     if (opt_x) {
  112.         dpy = XOpenDisplay(opt_d);
  113.         set_x_leds(LedModeOn);
  114.         XCloseDisplay(dpy);
  115.     }
  116. #endif
  117. #ifdef PARALLEL_SUPPORT
  118.     if (port_leds)
  119.         set_port_leds(LPT_PORT, port_leds);
  120. #endif
  121.     usleep(on_pause);
  122.     if (opt_t)
  123.         set_kbd_leds(0xff);
  124. #ifdef X_SUPPORT
  125.     if (opt_x) {
  126.         dpy = XOpenDisplay(opt_d);
  127.         set_x_leds(LedModeOff);
  128.         XCloseDisplay(dpy);
  129.     }
  130. #endif
  131. #ifdef PARALLEL_SUPPORT
  132.     if (port_leds)
  133.         unset_port_leds(LPT_PORT, port_leds);
  134. #endif
  135.     usleep(off_pause);
  136. }
  137.  
  138. void blink_x(on_pause, off_pause, x)
  139. unsigned long on_pause;
  140. unsigned long off_pause;
  141. int x;
  142. {
  143.     for (; x && !exit_now; x--)
  144.         blink_once(on_pause, off_pause);
  145. }
  146.